# -*- coding:utf-8 -*-

from application import app, db
from flask import Blueprint, request, jsonify, make_response, g, redirect
from common.libs.Helper import ops_render,isNull,getCurrentDate
from common.libs.admin.UserService import UserService
from common.models.admin.erp_user import ErpUser
from common.models.admin.erp_menu import ErpMenu
import json

route_user = Blueprint('user_page', __name__)


@route_user.route("/login.go", methods=["GET", "POST"])
def index():
    if request.method == "GET":#渲染还可以这样写的 同时处理cookie
        response = make_response(ops_render("/admin/user/login.html"))
        response.delete_cookie(app.config['AUTH_COOKIE_NAME'])
        return response

    resp = {'code': 200, 'msg': '登录成功~~', 'data': {}}
    req = request.values
    login_name = req['login_name'] if 'login_name' in req else ''
    login_pwd = req['login_pwd'] if 'login_pwd' in req else ''

    if isNull(login_name):
        resp['code'] = -1
        resp['msg'] = "请输入正确的登录用户名~~"
        return jsonify(resp)

    if isNull(login_pwd):
        resp['code'] = -1
        resp['msg'] = "请输入正确的账户密码~~"
        return jsonify(resp)

    user_info = ErpUser.query.filter_by(login_name=login_name).first()
    if not user_info:
        resp['code'] = -1
        resp['msg'] = "请输入正确的登录用户名和密码-1~~"
        return jsonify(resp)

    if user_info.login_pwd != UserService.genePwd(login_pwd, user_info.login_salt):
        resp['code'] = -1
        resp['msg'] = "请输入正确的登录用户名和密码-2~~"
        return jsonify(resp)

    if user_info.status != 1:
        resp['code'] = -1
        resp['msg'] = "账号已被禁用，请联系管理员处理~~"
        return jsonify(resp)

    if user_info.rid is None or len(user_info.rid)<1:
        resp['code'] = -1
        resp['msg'] = "账号异常，请联系管理员处理~~"
        return jsonify(resp)

    resp['data'] = {
        "token": UserService.genePwd(login_name, user_info.login_salt),
        "user": {
            "login_name": user_info.nickname
        }
    }

    response = make_response(json.dumps(resp))
    response.set_cookie(app.config['AUTH_COOKIE_NAME'], '%s#%s' % (
        UserService.geneAuthCode(user_info), user_info.uid), 60 * 60 * 24 * 120)  # 保存120天
    return response

@route_user.route("/edit.go", methods=["GET","POST"])
def edit():
    if request.method == "GET":
        resp_data = {}
        resp_data['current_user'] = g.current_user
        req = request.args
        return ops_render("/admin/user/edit.html", resp_data)
    resp = {'code': 200, 'msg': '用户信息修改成功~~', 'data': {}}
    req = request.values

    nickname = req['nickname'] if 'nickname' in req else ''
    mobile = req['mobile'] if 'mobile' in req else ''
    email = req['email'] if 'email' in req else ''
    sex = req['sex'] if 'sex' in req else 1

    if isNull(nickname):
        resp['code'] = -1
        resp['msg'] = "昵称不能为空哦~~"
        return jsonify(resp)

    user_info = g.current_user
    user_info.nickname = nickname
    user_info.mobile = mobile
    user_info.email = email
    user_info.sex = sex
    user_info.updated_time = getCurrentDate()

    db.session.add(user_info)
    db.session.commit()
    response = make_response(json.dumps(resp))
    return response

@route_user.route("/pwd.go", methods=["GET","POST"])
def resetPwd():
    if request.method == "GET":
        resp_data = {}
        req = request.args
        return ops_render("/admin/user/pwd.html", resp_data)

    resp = {'code': 200, 'msg': '密码修改成功~~', 'data': {}}
    req = request.values
    ymm = req['ymm'] if 'ymm' in req else ''
    xmm = req['xmm'] if 'xmm' in req else ''
    cmm = req['cmm'] if 'cmm' in req else ''

    if isNull(ymm):
        resp['code'] = -1
        resp['msg'] = "请输入正确的原密码-1~~"
        return jsonify(resp)
    if isNull(xmm):
        resp['code'] = -1
        resp['msg'] = "请输入正确的新密码~~"
        return jsonify(resp)
    if len(xmm)<6:
        resp['code'] = -1
        resp['msg'] = "新密码不能小于6位~~"
        return jsonify(resp)
    if isNull(cmm):
        resp['code'] = -1
        resp['msg'] = "请重复输入正确的新密码~~"
        return jsonify(resp)
    if xmm!=cmm:
        resp['code'] = -1
        resp['msg'] = "两次输入的新密码不一致~~"
        return jsonify(resp)

    user_info = g.current_user
    if user_info.login_pwd != UserService.genePwd(ymm, user_info.login_salt):
        resp['code'] = -1
        resp['msg'] = "请输入正确的原密码-2~~"
        return jsonify(resp)

    user_info.login_pwd = UserService.genePwd(xmm, user_info.login_salt)
    user_info.updated_time = getCurrentDate()

    db.session.add(user_info)
    db.session.commit()
    response = make_response(json.dumps(resp))
    return response
